// pages/home/home.js
var wxCharts = require('../../utils/wxcharts')
const app = getApp()
var orderringChart = null
var outputlineChart = null

Page({

  /**
   * 页面的初始数据
   */
  data: {
    /**
     * 页面宽度
     */
    windowWidth: 320,
    /**
     * 订单完成概况图的数据
     */
    orderringData: {
      title: {
        name: '100%',
        color: '#7cb5ec',
        fontSize: 25
      },
      subtitle: {
        name: '交货率',
        color: '#666666',
        fontSize: 15
      },
      series: [{
        name: '已完成',
        data: 10,
        stroke: false,
        color: '#00dd00'
      }, {
        name: '未完成',
        data: 0,
        stroke: false,
        color: '#dddddd'
      }, {
        name: '已延期',
        data: 0,
        stroke: false,
        color: '#ff0000'
      }]
    },
    /**
     * 订单出货量图的数据
     */
    outputlineData: {
      series: [{
        name: '出货量',
        data: [0, 0, 4828, 16228, 25628, 63998, 3132],
        color: '#00dd00'
      }],
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    try {
      var res = wx.getSystemInfoSync()
      this.data.windowWidth = res.windowWidth
    } catch (e) {
      console.error('getSystemInfoSync failed!')
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    orderringChart = new wxCharts({
      animation: true,
      canvasId: 'orderRing',
      type: 'ring',
      extra: {
        ringWidth: 6,
        pie: {
          offsetAngle: -45
        }
      },
      title: {
        name: '100%',
        color: '#7cb5ec',
        fontSize: 25
      },
      subtitle: {
        name: '交货率',
        color: '#666666',
        fontSize: 15
      },
      series: [{
        name: '已完成',
        data: 10,
        stroke: false,
        color: '#00dd00'
      }, {
        name: '未完成',
        data: 0,
        stroke: false,
        color: '#dddddd'
      }, {
        name: '已延期',
        data: 0,
        stroke: false,
        color: '#ff0000'
      }, ],
      disablePieStroke: true,
      width: this.data.windowWidth,
      height: 240,
      dataLabel: false,
      legend: false,
      background: '#f5f5f5',
      padding: 0
    })
    outputlineChart = new wxCharts({
      canvasId: 'output',
      type: 'line',
      categories: [
        '2018-10-14',
        '2018-10-15',
        '2018-10-16',
        '2018-10-17',
        '2018-10-18',
        '2018-10-19',
        '2018-10-20'
      ],
      animation: true,
      // background: '#f5f5f5',
      series: [{
        name: '出货量',
        data: [0, 0, 4828, 16228, 25628, 63998, 3132],
        color: '#00dd00'
      }],
      xAxis: {
        disableGrid: true
      },
      yAxis: {
          title: '出货量',
          format: function (val) {
              return val;
          },
          min: 0
      },
      width: this.data.windowWidth - 20,
      height: 240,
      dataLabel: false,
      dataPointShape: true,
      extra: {
          lineStyle: 'curve'
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  /**
   * 点击订单完成概况环状图的监听事件
   */
  touchOrderringHandler: function (e) {
    console.log(orderringChart.getCurrentDataIndex(e))
    this.updateOrderringChartData()
  },

  /**
   * 点击出货量图的监听事件
   */
  touchOutputHandler: function (e) {
    this.updateOutputlintChartData()
  },

  /**
   * 更新订单完成概况图
   */
  updateOrderringChartData: function () {
    // TODO: 从后端请求数据
    orderringChart.updateData(this.data.orderringData)
  },

  /**
   * 更新出货量图
   */
  updateOutputlintChartData: function () {
    // TODO: 从后端请求数据
    outputlineChart.updateData(this.data.outputlineData)
  }
})